<?php

require_once dirname(__FILE__) . '/../lib/BaseygmPerModuleActions.class.php';

/**
 * ygmPerModule actions.
 * 
 * @package    ygmPermissionSystemPlugin
 * @subpackage ygmPerModule
 * @author     Yuri Gauer Marques <yurigmarques@gmail.com>
 * @version    SVN: $Id: actions.class.php 12534 2008-11-01 13:38:27Z Kris.Wallsmith $
 */
class ygmPerModuleActions extends BaseygmPerModuleActions {

    protected function processForm(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            $notice = $form->getObject()->isNew() ? 'The item was created successfully.' : 'The item was updated successfully.';

            try {

                if ($request->getParameter('id')) {
                    $ygm_per_moduleAntes = Doctrine_Core::getTable('ygmPerModule')
                                    ->find($request->getParameter('id'))->ygmPerModuleCredential;

                    $request->setParameter('credential_anterior', $ygm_per_moduleAntes);
                }

                $ygm_per_module = $form->save();
                $request->setParameter('result_save', $ygm_per_module);
                $moduleCredential = new YgmPerModuleCredential();
                $moduleCredential->setPermissions($request);
            } catch (Doctrine_Validator_Exception $e) {

                $errorStack = $form->getObject()->getErrorStack();

                $message = get_class($form->getObject()) . ' has ' . count($errorStack) . " field" . (count($errorStack) > 1 ? 's' : null) . " with validation errors: ";
                foreach ($errorStack as $field => $errors) {
                    $message .= "$field (" . implode(", ", $errors) . "), ";
                }
                $message = trim($message, ', ');

                $this->getUser()->setFlash('error', $message);
                return sfView::SUCCESS;
            }

            $this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $ygm_per_module)));

            if ($request->hasParameter('_save_and_add')) {
                $this->getUser()->setFlash('notice', $notice . ' You can add another one below.');

                $this->redirect('@ygm_per_module_ygmPerModule_new');
            } else {
                $this->getUser()->setFlash('notice', $notice);

                $this->redirect(array('sf_route' => 'ygm_per_module_ygmPerModule_edit', 'sf_subject' => $ygm_per_module));
            }
        } else {
            $this->getUser()->setFlash('error', 'The item has not been saved due to some errors.', false);
        }
    }

    public function executeDelete(sfWebRequest $request) {
        $request->checkCSRFProtection();

        $moduleCredential = new YgmPerModuleCredential();
        //---------
        $moduleCredential->deletePermissionsModule($this->getRoute()->getObject());


        $this->dispatcher->notify(new sfEvent($this, 'admin.delete_object', array('object' => $this->getRoute()->getObject())));

        if ($this->getRoute()->getObject()->delete()) {
            $this->getUser()->setFlash('notice', 'The item was deleted successfully.');
        }

        $this->redirect('@ygm_per_module_ygmPerModule');
    }

    protected function executeBatchDelete(sfWebRequest $request) {
        $ids = $request->getParameter('ids');

        $records = Doctrine_Query::create()
                ->from('ygmPerModule')
                ->whereIn('id', $ids)
                ->execute();

        foreach ($records as $record) {
            $moduleCredential = new YgmPerModuleCredential();
            //---------
            $moduleCredential->deletePermissionsModule($record);
            $record->delete();
        }

        $this->getUser()->setFlash('notice', 'The selected items have been deleted successfully.');
        $this->redirect('@ygm_per_module_ygmPerModule');
    }

}
